<template>
  <vxe-modal
    title="办理任务"
    v-model="dialogVisible"
    v-if="dialogVisible"
    :loading="loading"
    width="70%" height="87vh" showFooter esc-closable show-zoom resize>

    <div style="height: 52vh; overflow-y: auto;">
      <el-card class="box-card" shadow="never" :body-style="{ padding: '10px',backgroundColor: '#bfcdda' }">
        <!-- 基本信息 -->
        <el-card class="box-card" shadow="hover">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff;">基本信息</span>
          </div>
          <el-descriptions class="margin-top" :column="2" size="medium" border
                           :contentStyle="content_style" :labelStyle="label_style">
            <el-descriptions-item>
              <template slot="label">
                <div class="right-align-label">
                  <i class="el-icon-user"></i>
                  申报年度：
                </div>
              </template>
              {{ cxdwData.sbNd }}
            </el-descriptions-item>
          </el-descriptions>
        </el-card>
        <br>
        <!-- 资料清单 -->
        <el-card class="box-card" shadow="hover" style="min-height: 25vh">
          <div slot="header" class="clearfix">
            <span style="font-size: 16px; color: #1890ff;">资料清单</span>
          </div>

          <el-row :gutter="20">
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-tree
                  :data="fileOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  node-key="id"
                  default-expand-all
                  highlight-current
                  @node-click="handleNodeClick"
                  :render-content="renderContent"
                />
              </div>
            </el-col>
            <el-col :span="20" :xs="24">
              <el-table v-loading="loadingFj" height="30vh"
                        :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                        :data="cxdwFjList">
                <el-table-column label="序号" align="center" type="index" width="50"/>
                <el-table-column label="文件名称" align="center" :formatter="getFileNames" prop="fileName"/>
                <el-table-column label="文件大小" align="center" prop="fileSize" width="150"/>
                <el-table-column label="操作" align="center" width="150">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-view"
                      @click="handlePreview(scope.row)"
                    >预览
                    </el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleDownload(scope.row)"
                    >下载
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </el-card>
        <br>

      </el-card>
    </div>
    <el-divider></el-divider>

    <el-card shadow="never">
      <el-form label-width="100px">
        <el-form-item label="审核状态：">
          <el-radio label="1" v-model="submitType">同意</el-radio>
          <el-radio label="2" v-model="submitType">驳回</el-radio>
          <el-select remote v-if="submitType==='2'" v-model="denyTo" placeholder="驳回到："
                     @visible-change="selectBefore">
            <el-option
              v-for="item in beforeData"
              :key="item.dkey"
              :label="item.taskName"
              :value="item.dkey">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审核意见：">
          <el-input type="textarea" v-model="comment"></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <div slot="footer" class="dialog-footer dialog-footer-center">
      <el-button type="primary" @click="onSubmit()">提交</el-button>
      <el-button @click="dialogVisible=false">关闭</el-button>
    </div>

    <!-- 预览组件 -->
    <KkFileView ref="preview"/>
  </vxe-modal>
</template>
<script>

import {fallBackTaskByTaskDefinitionKey, passTask, selectBefore, selectHistory} from "@/api/activiti/task"
import {getCxdw, selectCxdwFjList} from "@/api/khpx/cxdw";
import KkFileView from "@/views/components/KKFileView/kkFileView.vue";

export default {
  name: "CxDwSq",
  components: {KkFileView},
  //创新单位审批界面
  dicts: ['sys_user_sex', 'rcpt_zc', 'rcpt_sxly', 'rcpt_ryfl', 'rcpt_sf'],
  data() {
    return {
      loadingFj: false,
      cxdwFileType: "申报书", //默认的附件类型
      defaultProps: {children: "children", label: "label"}, // 表单参数
      fileOptions: [{ //附件类型树表
        id: 1,
        label: '1.申报书',
        type: '申报书'
      }, {
        id: 2,
        label: '2.知识产权',
        type: '知识产权'
      }, {
        id: 3,
        label: '3.工法论文',
        type: '工法论文'
      }, {
        id: 4,
        label: '4.科技奖项',
        type: '科技奖项'
      }, {
        id: 5,
        label: '5.其他证明',
        type: '其他证明'
      }],
      loading: false,
      cxdwData: [], //创新单位基本信息
      cxdwFjList: [], //创新单位附件信息
      cxdwId: null, //创新单位主键ID
      beforeData: [],
      beforeDataLoading: false,
      denyTo: null,
      dialogVisible: false,
      rowData: null,
      submitType: "1",//默认同意
      comment: null,//审批意见
      businessData: null,//业务数据
      historyList: [],//历史信息
      content_style: {
        // 解决不对齐的问题
        'width': "400px",
        // 排列第二行
        'word-break': 'break-all',
        // 'background-color': '#f3f7ff'
      },
      label_style: {
        // 解决不对齐的问题
        'width': "250px",
        // 排列第二行
        'word-break': 'break-all',
      },
    };
  },
  created() {
  },
  methods: {
    // 打开预览组件
    handlePreview(row) {
      this.$refs.preview.handleOpen(row);
    },
    // 节点单击事件
    handleNodeClick(data, node) {
      this.loadingFj = true
      let cxdwId = this.cxdwId
      if (node.level > 0 && node.data.children && node.data.children.length > 0) { // 检查是否有子节点
        const firstChildNode = node.data.children[0]; // 获取第一个子节点
        this.$refs.tree.setCurrentKey(firstChildNode.id); // 设置当前选中的节点为第一个子节点
        data = firstChildNode; // 更新 data 为第一个子节点的数据
        this.cxdwFileType = data.type
        selectCxdwFjList(cxdwId, this.cxdwFileType).then(resFileList => {
          this.cxdwFjList = resFileList.data;
          this.loadingFj = false
        })
      } else {
        //根据附件类型查询附件列表
        this.cxdwFileType = data.type
        selectCxdwFjList(cxdwId, this.cxdwFileType).then(resFileList => {
          this.cxdwFjList = resFileList.data;
          this.loadingFj = false
        })
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    handleOpen(row) {
      this.rowData = row
      this.loading = true;
      this.cxdwId = row.businessKey
      this.dialogVisible = true
      this.getCxDwData()
    },
    handleNodeClicks(data) {
      this.loadingFj = true
      let cxdwId = this.cxdwId
      this.cxdwFileType = data.type
      selectCxdwFjList(cxdwId, this.cxdwFileType).then(resFileList => {
        this.cxdwFjList = resFileList.data;
        this.loadingFj = false
        this.loading = false
      })
    },
    //获取详情数据
    getCxDwData() {
      const cxdwId = this.cxdwId
      getCxdw(cxdwId).then(res => {
        this.cxdwData = res.data

        selectCxdwFjList(cxdwId, this.cxdwFileType).then(resFileList => {
          console.log(resFileList, 'adsadsads')
          this.cxdwFjList = resFileList.data;
        })

        //点击树表第一个节点
        this.$nextTick(() => {
          const firstNode = this.$refs.tree.store.nodesMap['1']; // 默认点击id为1的节点
          if (firstNode) {
            this.$refs.tree.setCurrentKey(firstNode.key);
            this.handleNodeClicks(firstNode.data);
          }
        });

      })
    },
    //获取之前的节点信息
    selectBefore(visible) {
      //console.log(this.rowData.taskId, "======selectBefore")
      if (visible) {
        this.beforeDataLoading = true;
        this.denyTo = null;
        this.beforeData = [];
        selectBefore({
          taskId: this.rowData.taskId,
          comment: this.comment,
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId
        }).then(res => {
          console.table(res);
          setTimeout(() => {
            this.beforeData = res.data;
            this.beforeDataLoading = false;
          }, 100); // 模拟异步加载数据
          //this.$options = res.data
        })
      }
    },
    clickTab(tab, event) {
      if (tab.name === 'history') {
        this.getSelectHistory()
      }
    },
    //获取历史记录
    getSelectHistory() {
      selectHistory({processInstanceId: this.rowData.processInstanceId}).then(res => {
        this.historyList = res.data
      })
    },
    //提交
    onSubmit() {
      if (!this.submitType) {
        this.$message.warning("请选择提交类型")
        return
      }
      if (!this.comment) {
        this.$message.warning("请输入审批意见")
        return
      }
      if (this.submitType == 1) {
        console.log(this.rowData, '1111111111111111111111111111111')
        //审核通过
        passTask({
          taskId: this.rowData.taskId,
          comment: this.comment,
          params: {
            // day: this.businessData.qingJiaDay
          },
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId
        }).then(res => {
          this.dialogVisible = false
          this.$emit('ok')
        })
      }
      //任务驳回
      if (this.submitType == 2) {
        console.log("nnnnnnnnn" + this.denyTo);
        //审核不通过
        fallBackTaskByTaskDefinitionKey({
          comment: this.comment,
          businessKey: this.rowData.businessKey,
          businessType: this.rowData.businessType,
          processInstanceId: this.rowData.processInstanceId,
          taskId: this.rowData.taskId,
          taskDefinitionKey: this.denyTo
        }).then(res => {
          console.log(res, "[[[[")
          this.dialogVisible = false
          this.denyTo = null;
          this.beforeData = [];
          this.$emit('ok')
        })
      }
    },

  }
};
</script>

